package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.*;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

//套餐的业务层实现类
@Service
@Transactional
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;

    //分页查询套餐
    @Override
    @Cacheable(value = "setmeal",key = "'findPage'")
    public Page<Setmeal> findByPage(Integer pageNum, Integer pageSize, String name) {
        //根据姓名模糊查询列表
        LambdaQueryWrapper<Setmeal> qw01 = new LambdaQueryWrapper<>();
        qw01.like(name != null && name.length() > 0, Setmeal::getName, name);
        //开启分页          select * from setmeal
        //                  <where>
        //                      <if test = "name != null && name.length() > 0">
        //                         and name = #{name}
        //                      <if>
        //                  <where>
        Page<Setmeal> page = new Page<>(pageNum, pageSize);
        page = setmealMapper.selectPage(page, qw01);
        //遍历套餐
        List<Setmeal> setmealList = page.getRecords();
        for (Setmeal setmeal : setmealList) {
            //根据套餐id查询分类信息
            Long id = setmeal.getCategoryId();
            Category category = categoryMapper.selectById(id);
            setmeal.setCategoryName(category.getName());
            //根据套餐id查询套餐和菜品的中间表
            LambdaQueryWrapper<SetmealDish> qw02 = new LambdaQueryWrapper<>();
            qw02.eq(SetmealDish::getSetmealId, setmeal.getId());
            List<SetmealDish> setmealDishList = setmealDishMapper.selectList(qw02);
            setmeal.setSetmealDishes(setmealDishList);
        }
        return page;
    }

    //套餐新增
    @Override
    @CacheEvict(value = "setmeal", key = "#setmeal.categoryId")
    public void save(Setmeal setmeal) {
        //保存套餐信息
        setmealMapper.insert(setmeal);
        //遍历保存到套餐中的菜品信息
        List<SetmealDish> setmealDishList = setmeal.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishList) {
            //关联套餐id
            setmealDish.setSetmealId(setmeal.getId());
            //保存到套餐和菜品的中间表
            setmealDishMapper.insert(setmealDish);
        }
    }

    //修改套餐之回显套餐
    @Override
    @Cacheable(value = "setmeals",key = "#id")
    public Setmeal findById(Long id) {
        //根据套餐id调用mapper查询套餐
        Setmeal setmeal = setmealMapper.selectById(id);
        //根据套餐id查询中间表获得菜品信息
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> setmealDishes = setmealDishMapper.selectList(qw);
        //将集合存入setmeal对象中
        setmeal.setSetmealDishes(setmealDishes);
        //返回
        return setmeal;
    }

    //修改套餐
    @Override
    @CacheEvict(value = "setmeal", allEntries = true)
    public void update(Setmeal setmeal) {
        //保存修改后的套餐
        setmealMapper.updateById(setmeal);
        //将旧菜品列表删除
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId, setmeal.getId());
        setmealDishMapper.delete(qw);
        //遍历新的菜品
        for (SetmealDish setmealDish : setmeal.getSetmealDishes()) {
            //设置菜品id
            setmealDish.setSetmealId(setmeal.getId());
            //将新的菜品存入中间表
            setmealDishMapper.insert(setmealDish);
        }
    }


    //批量启停售
    @Override
    @CacheEvict(value = "setmeal", allEntries = true)
    public void stopBuy(Integer status, List<Long> ids) {
        //根据条件修改套餐的状态
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.in(Setmeal::getId, ids);
        //创建套餐对象
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        //调用mapper
        setmealMapper.update(setmeal, qw);
    }

    //批量删除
    @Override
    @CacheEvict(value = "setmeal", allEntries = true)
    public String delete(List<Long> ids) {
        String setmealDishName = "";
        //根据ids查询对应的套餐
        List<Setmeal> setmealList = setmealMapper.selectBatchIds(ids);
        //遍历集合
        for (Setmeal setmeal : setmealList) {
            //判断套餐是否为在售
            if (setmeal.getStatus() == 0) {
                //再删除对应的中间表信息
                LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
                qw.eq(SetmealDish::getSetmealId, setmeal.getId());
                setmealDishMapper.delete(qw);
                //套餐停售中,先删除套餐信息
                setmealMapper.deleteById(setmeal.getId());
            } else {
                setmealDishName = setmealDishName + setmeal.getName() + ",";

            }
        }
        return setmealDishName;
    }

    //点餐端根据分类id和状态码查询套餐列表
    @Override
    @Cacheable(value = "setmeal",key = "#categoryId")
    public List<Setmeal> findByCategoryAndStatus(Long categoryId) {
        //创建条件查询语句
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        //创建调价查询语句
        qw.eq(Setmeal::getCategoryId, categoryId);
        qw.eq(Setmeal::getStatus, 1);
        //调用mapper
        List<Setmeal> setmealList = setmealMapper.selectList(qw);
        //返回
        return setmealList;
    }

    //点餐端点击套餐图片显示套餐信息
    @Override
    @Cacheable(value = "setmeal",key = "#id")
    public List<Dish> findBySetmealAndDish(Long id) {
        //根据套餐id获取菜品信息
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        //创建调条件查询语句
        qw.eq(SetmealDish::getSetmealId, id);
        //调用mapper
        List<SetmealDish> setmealDishList = setmealDishMapper.selectList(qw);
        //创建空的菜品集合
        List<Dish> dishList = new ArrayList<>();
        //遍历中间表集合
        for (SetmealDish setmealDish : setmealDishList) {
            //创建条件查询对象
            //LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
            //创建条件查询语句
            //lqw.eq(Dish::getId, setmealDish.getDishId());
            //调用mapper
            //Dish dish = dishMapper.selectOne(lqw);
            //根据中间表菜品id查询菜品表
            Dish dish = dishMapper.selectById(setmealDish.getDishId());
            //设置菜品的数量
            dish.setCopies(setmealDish.getCopies());
            //添加进集合
            dishList.add(dish);
        }
        //返回
        return dishList;
    }







    //@Override
    //public Setmeal findById(Long id) {
    //    //1.根据id查询类型表格
    //    Setmeal setmeal = setmealMapper.selectById(id);
    //    //2.根据id查询类型菜品中间表
    //    LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
    //    wrapper.eq(SetmealDish::getSetmealId,id);
    //    List<SetmealDish> setmealDishList = setmealDishMapper.selectList(wrapper);
    //    //3.将集合存入setmeal对象中
    //    setmeal.setSetmealDishes(setmealDishList);
    //    //3.返回
    //    return setmeal;
    //}
}
